﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using yncTech.BusinessObjects;
using EntitySpaces.Interfaces;
using yncTech.Common;

public partial class Admin_Sys_Sec_Sec005a : AdminBasePage
{
    int userID = -1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[TSUsersMetadata.ColumnNames.UserID] != null)
        {
            userID = int.Parse(Request.QueryString[TSUsersMetadata.ColumnNames.UserID]);
        }

        if (!IsPostBack)
        {

            switch (ActionType)
            {
                case OperationType.Add:
                    btnSave.Text = "新增";
                    radActive.Checked = true;
                    break;
                case OperationType.Update:
                    if (userID > 0)
                    {
                        txtUser.Required = false;
                        txtUser.Enabled = false;

                        txtPwd.Required = false;
                        txtConPwd.Required = false;
                        if (LoginUserInfo.CurrentUser.UserID.Equals(userID))
                        {
                            radActive.Enabled = false;
                            radInactive.Enabled = false;
                        }

                        TSUserRoles ur = new TSUserRoles();
                        ur.Query.Where(ur.Query.UserID == userID);
                        ur.Query.es.Top = 1;
                        ur.Query.Load();
                        if (ur.es.HasData)
                        {
                            drpRole.SelectedValue = ur.RoleID.ToString();
                        }

                        TSUserInfo userInfo = new TSUserInfo();

                        if (userInfo.LoadByPrimaryKey(userID))
                        {
                            txtName.Text = userInfo.UserName;
                            txtMail.Text = userInfo.Email;
                            txtManager.Text = userInfo.Manager;
                            drpDept.SelectedValue = userInfo.DeptNo;
                            drpUnit.SelectedValue = userInfo.ThirdUnitID.ToString();

                            TSUsers user = new TSUsers();
                            if (user.LoadByPrimaryKey(userID))
                            {
                                txtUserID.Text = user.UserID.ToString();
                                txtUser.Text = user.LoginName;

                                if (user.IsActive.Value == (int)ActiveStatus.Active)
                                {
                                    radActive.Checked = true;
                                }
                                else
                                {
                                    radInactive.Checked = true;
                                }
                            }
                            var roles = user.UpToTSRoleCollection;
                            if (roles.Count > 0)
                            {
                                drpRole.SelectedValue = roles[0].RoleID.ToString();
                            }
                        }

                        btnSave.Text = "修改";
                    }
                    else
                    {
                        btnSave.Enabled = false;
                    }
                    break;
                case OperationType.Delete:
                    btnSave_Click(null, null);
                    break;
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            switch (ActionType)
            {
                case OperationType.Add:
                    using (esTransactionScope ts = new esTransactionScope())
                    {

                        TSUsers users = new TSUsers();
                        users.LoginName = txtUser.Text;
                        users.Password = txtPwd.Text;
                        users.UserType = ConstantValue.UserType.BOSCH;
                        users.IsActive = radActive.Checked ? (short)ActiveStatus.Active : (short)ActiveStatus.InActive;
                        users.LastUpdatedBy = users.CreatedBy = LoginUserInfo.CurrentUser.LoginName;
                        users.CreatedTime = users.LastUpdatedTime = DateTime.Now;

                        users.Save();

                        TSUserInfo userInfo = new TSUserInfo();
                        userInfo.UserID = users.UserID;
                        userInfo.ThirdUnitID = decimal.Parse(drpUnit.SelectedValue);
                        userInfo.UserName = txtName.Text;
                        userInfo.Email = txtMail.Text;
                        userInfo.DeptNo = drpDept.SelectedValue;
                        userInfo.Manager = txtManager.Text;
                        userInfo.LastUpdatedBy = userInfo.CreatedBy = LoginUserInfo.CurrentUser.LoginName;
                        userInfo.LastUpdatedTime = userInfo.CreatedTime = DateTime.Now;
                        userInfo.Save();

                        TSUserRoles uroles = new TSUserRoles();
                        uroles.UserID = userInfo.UserID;
                        uroles.RoleID = int.Parse(drpRole.SelectedValue);
                        uroles.Save();
                        ts.Complete();
                    }

                    break;
                case OperationType.Update:
                    using (esTransactionScope ts = new esTransactionScope())
                    {

                        TSUsers users = new TSUsers();
                        users.LoadByPrimaryKey(userID);
                        if (txtPwd.Text.Length > 0)
                        {
                            users.Password = txtPwd.Text;

                        }
                        users.IsActive = radActive.Checked ? (short)ActiveStatus.Active : (short)ActiveStatus.InActive;
                        users.LastUpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                        users.LastUpdatedTime = DateTime.Now;
                        users.Save();

                        TSUserRoles urole = new TSUserRoles();
                        urole.Query.Where(urole.Query.UserID == userID);
                        urole.Query.es.Top = 1;
                        urole.Query.Load();

                        urole.RoleID = int.Parse(drpRole.SelectedValue);
                        urole.Save();

                        TSUserInfo userInfo = new TSUserInfo();
                        userInfo.LoadByPrimaryKey(userID);
                        userInfo.ThirdUnitID = decimal.Parse(drpUnit.SelectedValue);
                        userInfo.UserName = txtName.Text;
                        userInfo.Email = txtMail.Text;
                        userInfo.Manager = txtManager.Text;
                        userInfo.LastUpdatedBy = userInfo.CreatedBy = LoginUserInfo.CurrentUser.LoginName;
                        userInfo.LastUpdatedTime = userInfo.CreatedTime = DateTime.Now;
                        userInfo.Save();
                        ts.Complete();
                    }
                    break;
                case OperationType.Delete:
                    using (esTransactionScope ts = new esTransactionScope())
                    {
                        TSUsers users = new TSUsers();
                        users.LoadByPrimaryKey(userID);
                        users.Password = txtPwd.Text;
                        users.IsActive = (int)ActiveStatus.Locked;
                        users.LastUpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                        users.LastUpdatedTime = DateTime.Now;

                        users.Save();

                        ts.Complete();

                    }
                    WebHelper.GotoParent();
                    break;
            }
            WebHelper.CloseDialog(true);
        }
        catch (Exception ex)
        {
            lbMessage.Text = ex.Message;
        }
    }
}